home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 October / Chip Ekim 2003.iso / prog / code / contr / setup.exe / Disk1 / data1.cab / Configuration_En / Commands / PasteManager.js < prev    next >
Encoding:
JavaScript  |  2003-07-18  |  11.3 KB  |  381 lines

  1. //=========================================================================================================
  2. //
  3. // Copyright 2002 Macromedia, Inc. All rights reserved.
  4. //
  5. // Feature: Paste Fix
  6. // Author:  JDH
  7. // Module:  PasteManager.js
  8. // Purpose:    Main Paste Manager class (and entry points).
  9. // Updates:
  10. //    5/17/02 - Started file control
  11. //    5/31/02 - Added more comments
  12. //
  13. //=========================================================================================================
  14.  
  15. //  The paste manager which handles the entire clipboard conversion process
  16.  
  17. function PasteManager()
  18. {
  19.     // Initialize the phases array
  20.  
  21.     this.phases = new Array();
  22.  
  23.     // Put together the initial list of content handlers
  24.  
  25.     var handlers = new Array();
  26.     handlers.push( new ParseMetaTags );
  27.     handlers.push( new IdentifyMSApplications );
  28.     handlers.push( new FixupMSGarbage );
  29.     handlers.push( new RetainStructure );
  30.     handlers.push( new DecomposeClasses );
  31.     handlers.push( new RemoveUnsupportedAttributes );
  32.     handlers.push( new RemoveParsingRequiredStructuralTags );
  33.     handlers.push( new RemoveCSSClasses );
  34.     handlers.push( new DemoteToParagraphs );
  35.     handlers.push( new SingleSpaceParagraphs );
  36.     handlers.push( new MergeRedundantFontTags );
  37.     handlers.push( new ChangeToStrongAndEm );
  38.     handlers.push( new ConvertEmptyDivs );
  39.  
  40.     // Iterate through the content handlers and put organize them by
  41.     // priority number into the phases member.
  42.  
  43.     for( index in handlers )
  44.     {
  45.         // Get the phase descriptor from the handler
  46.  
  47.         var phase_desc = handlers[ index ].getPhase();
  48.     
  49.         // Get the phase priority
  50.  
  51.         var phase = phase_desc.priority;
  52.  
  53.         // If the phase is currently empty then create the array to put the
  54.         // handlers into
  55.  
  56.         if ( this.phases[ phase ] == null )
  57.         {
  58.             this.phases[ phase ] = new Array();
  59.             this.phases[ phase ].name = phase_desc.name;
  60.             this.phases[ phase ].elements = new Array();
  61.         }
  62.  
  63.         // Push the handler into the handlers list for this phase number
  64.  
  65.         this.phases[ phase ].elements.push( handlers[ index ] );
  66.     }
  67. }
  68.  
  69. PasteManager.prototype.run = PasteManager_run;
  70. PasteManager.prototype.getClipHTML = PasteManager_getClipHTML;
  71. PasteManager.prototype.getDebugText = PasteManager_getDebugText;
  72. PasteManager.prototype.getDebugHTML = PasteManager_getDebugHTML;
  73.  
  74. function PasteManager_run( clipDOM, clipCSS, targetDOM, targetCSS, settings )
  75. {
  76.     // Put together the new context
  77.  
  78.     this.pasteContext = new PasteContext( clipDOM, clipCSS, targetDOM, targetCSS, settings );
  79.  
  80.     // Start the debug trace
  81.  
  82.     for( var setting in settings )
  83.         this.pasteContext.debugInformation( "PasteManager", "Setting : " + setting );
  84.     this.pasteContext.debugInformation( "PasteManager", "Start run" );
  85.  
  86.     // Iterate through the phases
  87.  
  88.     for ( var phase_index = 0; phase_index < PHASE_MAX; phase_index++ )
  89.     {
  90.         // If we have elements in this phase then iterate through them
  91.  
  92.         if ( this.phases[ phase_index ] )
  93.         {
  94.             // Put out the debugging information
  95.  
  96.             this.pasteContext.debugInformation( "PasteManager", "Phase: " + this.phases[ phase_index ].name );
  97.  
  98.             // Get the phase elements and interate through them and run each one
  99.  
  100.             var phase = this.phases[ phase_index ].elements;
  101.  
  102.             for( handler_index in phase )
  103.             {
  104.                 var handler = phase[ handler_index ];
  105.                 handler.run( this.pasteContext );
  106.             }
  107.         }
  108.     }
  109.  
  110.     // Finish the debug trace
  111.  
  112. if ( 0 ) alert( this.pasteContext.getClipText() );
  113.  
  114.     this.pasteContext.debugInformation( "PasteManager", "End run" );
  115.  
  116.     this.pasteContext.updateClipDOM();
  117.  
  118.     // Return the output size
  119.  
  120.     var findClippingScanner = new FindClippingScanner();
  121.     html = findClippingScanner.scan( this.pasteContext.getClipText(), this.pasteContext );
  122.     return html.length;
  123. }
  124.  
  125. function PasteManager_getDebugText( ) { return this.pasteContext.getDebugText(); }
  126.  
  127. function PasteManager_getDebugHTML( ) { return this.pasteContext.getDebugHTML(); }
  128.  
  129. function PasteManager_getClipHTML( )
  130. {
  131.     // JDH: This is pretty brute force.  First we turn the whole document into text, then we 
  132.     // find the start and end of the fragment, suck it out, and then remove the markers from the
  133.     // finished piece.
  134.  
  135.     var full_html = this.pasteContext.getClipText();
  136.  
  137.     full_html = full_html.replace( /[\r\n]/g, " " );
  138.  
  139.     var out_html = full_html.match( /\<body\>(.*?)\<\/body\>/g );
  140.     out_html = out_html.toString();
  141.  
  142.     out_html = out_html.replace( /\<body\>/, "" );
  143.     out_html = out_html.replace( /\<\/body\>/, "" );
  144.     out_html = out_html.replace( /\<\!\-\-(\s*)StartFragment(\s*)\-\-\>/, "" );
  145.     out_html = out_html.replace( /\<\!\-\-(\s*)EndFragment(\s*)\-\-\>/, "" );
  146.  
  147.     return out_html;
  148. }
  149.  
  150.  
  151.  
  152. function smartPaste( bSilent, inputDOM, returnValue )
  153. {
  154.     // Check to make sure we aren't trying to paste in something huge
  155.  
  156.     if ( bSilent == false )
  157.     {
  158.         var text = inputDOM.documentElement.outerHTML;
  159.         if ( text.length > MM.od_MaxThreshold)
  160.         {
  161.             alert( MM.MSG_odStop );
  162.             returnValue[ 0 ] = true;
  163.             return;
  164.         }
  165.         if ( text.length > MM.od_WarnThreshold )
  166.         {
  167.             if ( ! confirm( MM.MSG_odWarn ) )
  168.             {
  169.                 returnValue[ 0 ] = true;
  170.                 return;
  171.             }
  172.         }
  173.     }
  174.  
  175.     try {
  176.  
  177.         // Put together a new paste manager
  178.  
  179.         var mgr = new PasteManager();
  180.  
  181.         // Define the settings for the paste
  182.  
  183.         var settings = {};
  184.         settings[ SETTINGS_CONTRIBUTE ] = 1;
  185.  
  186.         // Look for Contribute specific settings
  187.  
  188.         if ( dreamweaver.appName == "Contribute" )
  189.         {
  190.             if ( dreamweaver.getDocumentDOM().getCCSharedSetting_TextOnlyInNonTemplates() &&
  191.                  !dreamweaver.getDocumentDOM().isTemplateInstance())
  192.                 settings[ SETTINGS_ETO ] = 1;
  193.  
  194.             if ( dreamweaver.getDocumentDOM().getCCSharedSetting_FontsEmitFontOrSpan() == "font" )
  195.                 settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] = 1;
  196.  
  197.             if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_FontsLetUserChange() )
  198.                 settings[ SETTINGS_NO_CSS ] = 1;
  199.  
  200.             if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_StyleHTMLHeadings() )
  201.                 settings[ SETTINGS_DEMOTE_TO_PARAGRAPHS ] = 1;
  202.  
  203.             // commented this out for bug: 85013 -- think we're going to decide that
  204.             // show CSS styles and insert CSS style should be separate. So we shouldn't disable
  205.             // inserting Word text with <span> tags even if we don't allow users to set CSS styles.
  206.  
  207. //            if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_StyleCSS() )
  208. //                settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] = 1;
  209.  
  210.             if ( dreamweaver.getDocumentDOM().getCCSharedSetting_SingleSpaceParagraphsCSS() )
  211.                 settings[ SETTINGS_SINGLE_SPACE_P ] = 1;
  212.         }
  213.  
  214.         // We always use <STRONG> and <EM> instead of <B> and <I>, respectively.
  215.         settings[ SETTINGS_USE_EMPHASIS ] = 1;
  216.  
  217.         // Debug code for settings 
  218.  
  219. if ( 0 ) {
  220. if( settings[ SETTINGS_ETO ] ) alert( "settings[ SETTINGS_ETO ]" );
  221. if( settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] ) alert( "settings[ SETTINGS_CHANGE_SPAN_TO_FONT ]" );
  222. if( settings[ SETTINGS_NO_CSS ] ) alert( "settings[ SETTINGS_NO_CSS ]" );
  223. if( settings[ SETTINGS_DEMOTE_TO_PARAGRAPHS ] ) alert( "settings[ SETTINGS_DEMOTE_TO_PARAGRAPHS ]" );
  224. if( settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] ) alert( "settings[ SETTINGS_CHANGE_SPAN_TO_FONT ]" );
  225. if( settings[ SETTINGS_SINGLE_SPACE_P ] ) alert( "settings[ SETTINGS_SINGLE_SPACE_P ]" );
  226. }
  227.  
  228.         // Get the CSS definitions from the clipboard
  229.  
  230.         var clipCSS = new CSSClassCollection();
  231.  
  232.         Utils_LoadCSSFromDOM( inputDOM, clipCSS );
  233.  
  234.         // Get the names of the classes in the target
  235.  
  236.         var targetCSS = new CSSReferenceClassCollection();
  237.  
  238.         // Here we add referenced classes to the target CSS representative.  All that we
  239.         // use is the class name.
  240.         var styles = dreamweaver.cssStylePalette.getStyles();
  241.         for ( var index in styles )
  242.             targetCSS.add( styles[ index ] );
  243.  
  244.         var outputSize = mgr.run( inputDOM, clipCSS, null, targetCSS, settings );
  245.  
  246.         if ( outputSize < 1 )
  247.             throw( "No HTML was generated" );
  248.  
  249.         dw.forceGarbageCollection();
  250.  
  251.         if ( dreamweaver.appName == "Contribute" )
  252.             formatSource( inputDOM );
  253.  
  254.     }
  255.     catch( e )
  256.     {
  257.         alert( MM.MSG_odProblem );
  258.         dw.getDocumentDOM().clipPasteText();
  259.         returnValue[ 0 ] = true;
  260.     }
  261.  
  262.     return true;
  263. }
  264.  
  265.  
  266. // =============================================================================================================
  267. // =============================================================================================================
  268. // 
  269. //  Test code
  270. //
  271. // =============================================================================================================
  272. // =============================================================================================================
  273.  
  274. function testPaste( testName, clipFile, settingsStr, targetDOM, debug, addHeaders, inputDOM )
  275. {
  276.   var totalTime = null;
  277.  
  278.   if ( addHeaders )
  279.     targetDOM.setView( "design" );
  280.  
  281.   if ( clipFile.length > 0 )
  282.     JDHUtils.copyFromFile( "c:/pf/clips/" + clipFile );
  283.  
  284.   // Read the clipboard into a string
  285.  
  286.   var str = JDHUtils.readHTMLClipboard();
  287.  
  288.   if ( str.length > 0 )
  289.   {
  290.     var prefixHTML = "";
  291.     if ( addHeaders )
  292.     {
  293.       prefixHTML += "<h1> Test: " + testName + "</h1>";
  294.       prefixHTML += "<font face=Courier>Clipboard File: " + clipFile + "</font><br>";
  295.       prefixHTML += "<font face=Courier>Settings: " + settingsStr + "</font><br>";
  296.       prefixHTML += "<hr>";
  297.     }
  298.  
  299.     // Build an "offscreen" DOM to parse the document
  300.  
  301.     if ( inputDOM == null )
  302.     {
  303.         var fwURL = dw.getConfigurationPath() + "/Shared/MM/Cache/empty.htm";
  304.         DWfile.write( fwURL, '' );
  305.         var clipDOM = dw.getDocumentDOM( fwURL );
  306.         clipDOM.documentElement.outerHTML = "";
  307.         clipDOM.body.innerHTML = str;
  308.     }
  309.     else
  310.         clipDOM = inputDOM;
  311.  
  312.     // Put together a new paste manager
  313.  
  314.     var mgr = new PasteManager();
  315.  
  316.     // Define the settings for the paste
  317.  
  318.     var settings = {};
  319.     settings[ SETTINGS_CONTRIBUTE ] = 1;
  320.  
  321.     settingsValues = settingsStr.split( "," );
  322.     for( setting_index in settingsValues )
  323.     {
  324.         var settingsValue = Utils_StripWhitespace( settingsValues[ setting_index ] );
  325.         settings[ eval( settingsValue ) ] = 1;
  326.     }
  327.  
  328.     // Get the CSS definitions from the clipboard
  329.  
  330.     var clipCSS = new CSSClassCollection();
  331.     Utils_LoadCSSFromDOM( clipDOM, clipCSS );
  332.  
  333.     // Get the names of the classes in the target
  334.  
  335.     var targetCSS = new CSSReferenceClassCollection();
  336.  
  337.     // Run the paste buffer converter
  338.  
  339.     var startTime = new Date();
  340.  
  341.     mgr.run( clipDOM, clipCSS, targetDOM, targetCSS, settings );
  342.  
  343.     var endTime = new Date();
  344.     totalTime = ( endTime.getTime() - startTime.getTime() ) / 1000.0;
  345.  
  346.     // Get the debug HTML
  347.  
  348.     var debugData = "";
  349.     if( debug )
  350.       debugData += "<hr><h1>DEBUG</h1>" + mgr.getDebugHTML();
  351.  
  352.     // Insert the paste buffer plus the debug data
  353.  
  354.     var clipHTML = mgr.getClipHTML();
  355.  
  356. // alert( "clipHTML: " + clipHTML );
  357.  
  358.     targetDOM.insertHTML( prefixHTML + clipHTML + debugData );
  359.   }
  360.   else
  361.   {
  362.     return null;
  363.   }
  364.  
  365.   return totalTime;
  366. }
  367.  
  368. function trySmartPaste()
  369. {
  370.     var result = testPaste( "", "", "", dw.getDocumentDOM(), false, false, null );
  371.     return ( result == null ) ? false : true;
  372. }
  373.  
  374. // =============================================================================================================
  375. // =============================================================================================================
  376. // 
  377. //  Test code
  378. //
  379. // =============================================================================================================
  380. // =============================================================================================================
  381.